import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main818 {
    public static void main(String[] args) throws IOException {
        File file = new File("1\\2021-08-18\\code","from.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        String name = file.getName();
        long length = file.length();

        System.out.println("文件名称："+name);
        System.out.println("文件大小："+length/1024.0+"KB");

        System.out.println("------文件内容--------");

        InputStreamReader readerStream = null;
        BufferedReader reader = null;
        try {
            readerStream = new InputStreamReader(new FileInputStream(file),
                    StandardCharsets.UTF_8);
            String line = null;
            reader = new BufferedReader(readerStream);
            while ((line = reader.readLine()) != null){
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
        }
        finally {
            reader.close();
            readerStream.close();
        }

        System.out.println("转存文件到 to.txt");

        File newFile = new File("1\\2021-08-18\\code", "to.txt");
        if (!newFile.exists()){
            newFile.createNewFile();
        }

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                new FileOutputStream(newFile,true),
                StandardCharsets.UTF_8);
        PrintWriter writer = new PrintWriter(outputStreamWriter);

        try {
            readerStream = new InputStreamReader(new FileInputStream(file),
                    StandardCharsets.UTF_8);
            String line = null;
            reader = new BufferedReader(readerStream);
            while ((line = reader.readLine()) != null){
                writer.println(line);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
        }
        finally {
            reader.close();
            readerStream.close();
        }
        outputStreamWriter.flush();
        writer.close();
        outputStreamWriter.close();
    }
}
